#coding:utf-8
'''
create on 2017-04-20
@author:sandy
'''

from pycommon import Const
from pycommon import Pack as packet
import logging
import ClientFactory
import ClientBuild

def loginServerRegister(client,pkt):
    sid,ip,port = packet.unpack("HsH",pkt)
    clients = ClientFactory.instance.getClients()
    code = Const.ERR_OK
    for c in clients:
        if c.getSId() == sid:
            code = Const.ERR_LOGIN2MGR_SID_FAIL
            break
        if c.getSIp() == ip and c.getSPort() == port:
            code = Const.ERR_LOGIN2MGR_OTH_FAIL
            break
    if code == Const.ERR_OK:
        client.login(sid,ip,port)
    else:
        pass
    logging.debug("login->loginmgr sid=%d ip=%s port=%d code=%d",sid,ip,port,code)
    client.sendPacket(ClientBuild.loginmgr(code))

__cmdTable = {
        Const.CMD_LG2LGM_LOGIN   : loginServerRegister
    }

def parse(client,cmd,pkt):
    logging.debug(u"ClientParse cmd = %d",cmd)
    func = __cmdTable.get(cmd)
    if not func:
        logging.error(u"cmd = %d not found",cmd);
    func(client,pkt)

if __name__ == "__main__":
    pass